package top.lixunda.ecommerce.server.good.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;
import top.lixunda.commerce.common.core.entity.MessageObject;
import top.lixunda.ecommerce.server.business.BaseController;
import top.lixunda.ecommerce.server.good.api.controller.IGoodDeliverController;
import top.lixunda.ecommerce.server.good.api.entity.db.GoodOrderDO;
import top.lixunda.ecommerce.server.good.api.entity.dto.DeliverGoodChangeDTO;
import top.lixunda.ecommerce.server.good.api.entity.dto.DeliverGoodDTO;
import top.lixunda.ecommerce.server.good.api.entity.vo.OrderDistinguishVO;
import top.lixunda.ecommerce.server.good.api.entity.vo.OrderTranceVO;
import top.lixunda.ecommerce.server.good.service.IGoodDeliverService;

import java.util.List;

/**
 * 商家发货相关操作
 *
 * @author lixunda
 * @version time:2020/3/5 15:37
 */
@Slf4j
@RestController
public class GoodDeliverController extends BaseController implements IGoodDeliverController {

    private final IGoodDeliverService goodDeliverService;

    @Autowired
    public GoodDeliverController(IGoodDeliverService goodDeliverService) {
        this.goodDeliverService = goodDeliverService;
    }

    /**
     * 对某个订单进行发货
     *
     * @param deliverGoodList 发货相关信息，包括物流信息
     * @return 返回订单发货后的订单基本信息
     */
    @Override
    public MessageObject<List<GoodOrderDO>> deliverGoodByOrderIds(List<DeliverGoodDTO> deliverGoodList) {
        List<GoodOrderDO> list = goodDeliverService.deliverGoodByOrderIds(deliverGoodList);
        return getSuccessMessageObject("发货成功", list);
    }

    /**
     * 修改发货后的订单发货信息
     *
     * @param deliverGoodChangeList 发货相关信息，包括物流信息
     * @return 返回订单发货后的订单基本信息
     */
    @Override
    public MessageObject<List<GoodOrderDO>> changeDeliverGoodByOrderIds(List<DeliverGoodChangeDTO> deliverGoodChangeList) {
        List<GoodOrderDO> list = goodDeliverService.changeDeliverGoodByOrderIds(deliverGoodChangeList);
        return getSuccessMessageObject("修改信息成功", list);
    }

    /**
     * 根据订单id取消发货信息
     *
     * @param orderIds 订单id
     * @return 是否取消成功
     */
    @Override
    public MessageObject<Boolean> cancelDeliverByOrderIds(List<Integer> orderIds) {
        boolean success = goodDeliverService.cancelDeliverByOrderIds(orderIds);
        return success ? getSuccessMessageObject("取消发货成功", true) :
                getErrorMessageObject("取消发货失败", false);
    }

    /**
     * 物流订单号识别
     *
     * @param code 物流订单号
     * @return 识别信息，可能多个
     */
    @Override
    public MessageObject<List<OrderDistinguishVO>> orderDistinguish(String code) {
        List<OrderDistinguishVO> list = goodDeliverService.orderDistinguish(code);
        return getSuccessMessageObject("成功", list);
    }

    /**
     * 用户调用，根据订单号查询物流信息
     *
     * @param orderId 订单id
     * @return 物流信息
     */
    @Override
    public MessageObject<OrderTranceVO> queryTranceByOrderIdUser(Integer orderId) {
        OrderTranceVO orderTranceVO = goodDeliverService.queryTranceByOrderIdUser(orderId);
        return getSuccessMessageObject("查询物流信息成功", orderTranceVO);
    }

    /**
     * 商铺用户调用，根据订单号查询物流信息
     *
     * @param orderId 订单id
     * @return 物流信息
     */
    @Override
    public MessageObject<OrderTranceVO> queryTranceByOrderIdStore(Integer orderId) {
        OrderTranceVO orderTranceVO = goodDeliverService.queryTranceByOrderIdStore(orderId);
        return getSuccessMessageObject("查询物流信息成功", orderTranceVO);
    }

    /**
     * 管理员用户调用，根据订单号查询物流信息
     *
     * @param orderId 订单id
     * @return 物流信息
     */
    @Override
    public MessageObject<OrderTranceVO> queryTranceByOrderIdAdmin(Integer orderId) {
        OrderTranceVO orderTranceVO = goodDeliverService.queryTranceByOrderIdAdmin(orderId);
        return getSuccessMessageObject("查询物流信息成功", orderTranceVO);
    }

    /**
     * 用户调用，根据订单号收货
     *
     * @param orderId 订单id
     * @return 收货是否成功
     */
    @Override
    public MessageObject<Boolean> takeDeliveryByOrderId(Integer orderId) {
        boolean success = goodDeliverService.takeDeliveryByOrderId(orderId);
        return success ? getSuccessMessageObject("收货成功", true) :
                getErrorMessageObject("收货失败", false);
    }

    /**
     * 用户调用，根据订单号收货，批量处理
     *
     * @param orderIds 订单id
     * @return 收货是否成功
     */
    @Override
    public MessageObject<Boolean> takeDeliveryByOrderIds(List<Integer> orderIds) {
        boolean success = goodDeliverService.takeDeliveryByOrderIds(orderIds);
        return success ? getSuccessMessageObject("收货成功", true) :
                getErrorMessageObject("收货失败", false);
    }
}
